﻿/// <reference path="jquery-1.7.2.js" />
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "")
};
//cookie设置
jQuery.cookie = function (b, k, n) {
    if (typeof k != "undefined") {
        n = n || {};
        if (k === null) {
            k = "";
            n.expires = -1
        }
        var f = "";
        if (n.expires && (typeof n.expires == "number" || n.expires.toUTCString)) {
            var g;
            if (typeof n.expires == "number") {
                g = new Date();
                g.setTime(g.getTime() + (n.expires * 24 * 60 * 60 * 1000))
            } else {
                g = n.expires
            }
            f = "; expires=" + g.toUTCString()
        }
        var m = n.path ? "; path=" + (n.path) : "";
        var h = n.domain ? "; domain=" + (n.domain) : "";
        var a = n.secure ? "; secure" : "";
        document.cookie = [b, "=", encodeURIComponent(k), f, m, h, a].join("")
    } else {
        var d = null;
        if (document.cookie && document.cookie != "") {
            var l = document.cookie.split(";");
            for (var j = 0; j < l.length; j++) {
                var c = l[j].trim();
                if (c.substring(0, b.length + 1) == (b + "=")) {
                    d = decodeURIComponent(c.substring(b.length + 1));
                    break
                }
            }
        }
        return d
    }
}

Array.prototype.remove = function (indexNum) {
    if (isNaN(indexNum) || indexNum > this.length) {
        return false;
    }
    this.splice(indexNum, 1);
}
		  
function checkss()
  {
      if (document.searchs.skey.value=="请输入关键字"){
	      alert("搜索关键字不能为空")
		  document.searchs.skey.focus();
		  return false
		    }
			      if (document.searchs.skey.value==""){
	      alert("搜索关键字不能为空")
		  document.searchs.skey.focus();
		  return false
		    }
		  return true
  }
  

function initClipBox(){
	document.write("<input class=\"clipInput\" value=\"" + location.href + "\" onclick=\"copyToClipBoard(this)\"></input>");
	document.write("<input type=\"button\" class=\"clipButton\" value=\"复制\" onclick=\"copyToClipBoard(this)\" />");
}

function copyToClipBoard(el){
	el.select();
	var clipBoardContent = document.title +'\r\n'+ location.href +'';
	alert('复制成功,发给你的好友一起分享吧!');
	window.clipboardData.setData("Text",clipBoardContent);
}

//添加到收藏夹
function add_favorite(sURL, sTitle) {
    try {
        window.external.addFavorite(sURL, sTitle);
    } catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        } catch (e) {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

//加载history信息
function load_reading_history() {
    var ck = $.cookie("read");
    if (ck == null || ck == "") {
        $("#history_read").empty().append("对不起，您没有任何浏览记录！");
        return;
    }
    var v = ck.split('$');

    if (v.length >= 1) {
        content = "<ul>";
        for (var i = v.length - 1; i >= 0; i--) {
            var c = v[i].split('|');
            if (typeof (c[0]) != "undefined") {
                var chapter_name = "";
                if (typeof (c[2]) != "undefined") {
                    chapter_name = unescape(c[2]);
                }

                content += "<li>";
                content += "<span class=\"h-name\"><a href='/comic/" + c[1] + "' target='_blank'>" + unescape(c[0]) + "</a></span>";
                content += "<span class=\"h-see\"><a style=\"color:blue\" href='/comic/" + c[1] + "/" + c[3] + "/?p=" + c[4] + "' target='_blank'>继续观看" + chapter_name + "</a></span>";
                content += "<a href=\"javascript:;\" class=\"clear-this\" onclick=\"remove_history_book(" + c[1] + ");\"><\/a>";
                content += "</li>";
            }
        }
        content += "</ul>";
        content += "<p class=\"h-clear\"><a href=\"javascript:void(0);\" onclick=\"clear_reading_history();\">\u6e05\u9664\u6240\u6709\u89c2\u770b\u8bb0\u5f55<\/a><\/p>";
        $("#history_read").empty().append(content);
    } else {
        $("#history_read").empty().append("对不起，您没有任何浏览记录！");
    }
}

//清除reading history
function clear_reading_history() {
    $.cookie("read", "", { path: "/", expires: 1 });
    $(".history-area").html("");
    load_reading_history();
    return false;
}

function remove_history_book(b_id) {
    var ck = $.cookie("mh_reading_history");
    if (ck == null) {
        return;
    }
    var history_list = eval(ck);
    if (history_list.length >= 1) {
        for (var i = 0; i < history_list.length; i++) {
            if (history_list[i].book_id == b_id) {
                history_list.remove(i);
                break;
            }
        }
        $.cookie("mh_reading_history", this.arrayToSource(history_list), { path: "/", expires: 2592000000 });
        load_reading_history();
    }
}

function arrayToSource(list) {
    var str = "";
    if (typeof (list) == "object" && typeof (list.splice) == 'function') {
        str += "[";

        for (var i = 0; i < list.length; i++) {
            if (typeof (list[i]) == 'object' && typeof (list[i].book_id) != 'undefined' && typeof (list[i].chapter_id) != 'undefined' && typeof (list[i].page_no) != 'undefined' && typeof (list[i].book_name) != 'undefined') {
                str += "{book_id:" + parseInt(list[i].book_id) + "," + "chapter_id:" + parseInt(list[i].chapter_id) + "," + "page_no:" + parseInt(list[i].page_no) + "," + "book_name:'" + escape(list[i].book_name) + "'";
                if (typeof (list[i].chapter_name) != 'undefined') {
                    str += "," + "chapter_name:'" + escape(list[i].chapter_name) + "'";
                } else {
                    str += "," + "chapter_name:''";
                }
                str += "}";
                if (i < list.length - 1) str += ",";
            }
        }
        str += "]";
    }
    return str;
}

//自动执行
$(function () {
    //顶部添加收藏
    $('#top_add_favorite').click(function () {
        add_favorite("http://www.yzmanga.com", "雨泽漫画");
        return false;
    });

    //顶部历史记录
    $(".history-item,.history-area").hover(function () {
        $(".history-item").addClass("hover");
        $('.history-area').show();
        load_reading_history();
    }, function () {
        $(".history-item").removeClass("hover");
        $(".history-area").hide();
    });

    $(".menu").each(function () {
        $(this).hover(function () {
            $(this).children(".menu-hd").addClass("hover");
            $(this).children(".menu-bd").show();
        }, function () {
            $(this).children(".menu-hd").removeClass("hover");
            $(this).children(".menu-bd").hide();
        });
    });

    //load_top_msg();
});


var xmlHttp;
var bShow = false;
var iLen = 0;
var iIndex = 0;
var sKey = "";

function fnGetResult() {
    if ($("#kw").val() == "") {
        return
    }
    if ($("#kw").val() == sKey) {
        return
    }
    sKey = $("#kw").val();
    $.post("/search.aspx", "s=1&key=" + escape(sKey), fnHandle, "json");
}
function fnHandle(data) {
    fnInitData(data)
}
function fnInitData(data) {
    iIndex = 0;
    if (eval(data) == 'undefined') {
        return false
    }
    var oData = eval(data);
    try {
        iLen = oData.length
    } catch (e) { }
    if (iLen == 0) {
        $("#suggest").hide();
        bShow = false
    } else {
        var sRet = "<ul>";
        for (var i = 0; i < iLen; i++) {
            sRet += "<li onclick=\"fnGoTo(" + oData[i].id + ")\" onmouseover=\"this.className=\'on\'\" onmouseout=\"this.className=''\">" + oData[i].status + oData[i].title + "</li>"
        }
        sRet += "</ul>";
        sRet += "<div><a href='javascript:void(0);' onclick='fnClose()'>×关闭</a></div>";
        $("#suggest").html(sRet);
        $("#suggest").show();
        bShow = true
    }
}
function fnGoTo(id) {
    window.location.href = "/comic/" + id
}
function fnKeyup(e) {
    var iKey = (window.event) ? e.keyCode : ((e.which) ? e.which : 0);
    if (iKey == 40 || iKey == 38) {
        iIndex = (iKey == 40) ? ((iIndex >= iLen) ? 0 : ++iIndex) : ((iIndex <= 0) ? iLen : --iIndex);
        fnToogle(iIndex)
    } else {
        window.setTimeout(fnGetResult, 300)
    }
}
function searchSend() {
    if (iIndex > 0) {
        $("#suggest li:eq(" + (iIndex - 1) + ")").onclick()
    } else {
        if ($('#kw').val() == '') {
            window.alert('请输入您要查找的漫画！');
            $('#kw').focus();
            return false
        }
        window.location.href = '/search.aspx?key=' + $("#kw").val();
    }
}
function fnKeydown(e) {
    var iKey = (window.event) ? e.keyCode : ((e.which) ? e.which : 0);
    if (iKey == 13) {
        (iIndex > 0) ? $("#suggest")[0].getElementsByTagName("li")[iIndex - 1].onclick() : searchSend()
    }
}
function fnToogle(p) {
    var oSuggest = $("#suggest li");
    for (var i = 0; i < iLen; i++) {
        oSuggest[i].className = ""
    }
    if (p > 0) {
        oSuggest[p - 1].className = "on";
		$('#kw').val(oSuggest[p - 1].getElementsByTagName("p")[0].innerText);
    }
}
function fnFocus() {
    if ($('#kw').val() == '请输入搜索内容') {
        $('#kw').attr("style", "color:#333333")
        $('#kw').val("");
    }
    setTimeout(fnGetResult, 500);
    if (bShow) {
        $("#suggest").show();
    }
}
function fnBlur() {
    setTimeout(fnClose, 500)
}
function fnClose() {
    $("#suggest").hide();
}

function toFav() {
    document.writeln("<a class='baiduFav' href=\"javascript:window.open(\'http:\/\/cang.baidu.com\/do\/add?it=\'+encodeURIComponent(document.title.substring(0,76))+\'&iu=\'+encodeURIComponent(location.href)+\'&fr=ien#nw=1\',\'_blank\',\'scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes\');void(0)\" title='添加到百度搜藏'><span>添加到百度搜藏</span><\/a>");
    document.writeln("<a class='qqFav' href=\"javascript:window.open(\'http:\/\/shuqian.qq.com\/post?title=\'+encodeURIComponent(document.title)+\'&uri=\'+encodeURIComponent(document.location.href)+\'&jumpback=2&noui=1\',\'favit\',\'width=960,height=600,left=50,top=50,toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes\');void(0)\" title='收藏到QQ书签'><span>收藏到QQ书签</span><\/a>");
}

function changeTab(a, b) {
    var l = a - 1;
    var o = $('#tabList')[0].getElementsByTagName('li');
    var p = $('#comicList')[0].getElementsByTagName('ul');
    for (var i = 0; i < a; i++) {
        p[i].style.display = 'none';
        o[i].className = (i == l) ? 'end' : ''
    }
    o[b].className = (l == b) ? 'end on' : 'on';
    p[b].style.display = 'block'
}

function openWindow(b, a, c) {
    var e = (window.screen.availHeight - 30 - c) / 2;
    var d = (window.screen.availWidth - 10 - a) / 2;
    window.open(b, "", "height=" + c + ",,innerHeight=" + c + ",width=" + a + ",innerWidth=" + a + ",top=" + e + ",left=" + d + ",toolbar=no,menubar=no,scrollbars=no,resizeable=no,location=no,status=no");
    return false
}

function hotComic() {
    var arrLink = new Array();
    arrLink[0] = [4681, "火影忍者"];
    arrLink[1] = [4682, "死神"];
    arrLink[2] = [1128, "海贼王"];
    arrLink[3] = [2027, "名侦探柯南"];
    arrLink[4] = [2592, "七龙珠"];
    arrLink[5] = [3746, "网球王子"];
    arrLink[6] = [7649, "杀戮都市"];
    arrLink[7] = [7382, "斗罗大陆"];
    arrLink[8] = [7634, "偷星九月天"];
    arrLink[9] = [7620, "斗破苍穹"];
    arrLink[10] = [7675, "暗夜协奏曲"];
    document.write("热门漫画：");
    for (var i = 0; i < arrLink.length; i++) {
        document.write("<a href=\"/comic/" + arrLink[i][0] + "\" alt=\"" + arrLink[i][1] + "\">" + arrLink[i][1] + "</a> ");
    }
}